<template>
  <el-dialog title="修改管理员信息"
             :visible.sync="visible"
             @close="handleCancelEdit">
    <el-form :model="updateTemp"
             ref="updateTemp"
             :rules="rules"
             status-icon
    >
      <el-form-item label="用户名" prop="account">
        <el-input v-model="updateTemp.account" type="text" >
        </el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="updateTemp.phone" type="text" >
        </el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="updateTemp.email" type="text" >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancelEdit" v-waves>取消</el-button>
      <el-button type="primary" @click="handleUpdateData" v-waves>提交修改</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import waves from '@/directive/waves/index.js'

  export default {
    name: 'update-dialog',
    data() {
      return {
        visible: false,
        rules: {
          account: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
          ]
        }
      }
    },
    directives: {
      waves
    },
    props: {
      updateTemp: Object,
      dialogVisible: Boolean
    },
    watch: {
      dialogVisible(val) {
        this.visible = val
      }
    },
    methods: {
      handleCancelEdit() {
        this.visible = false
        this.$refs.updateTemp.resetFields()
        this.$emit('cancel', this.visible)
      },
      handleUpdateData() {
        this.$refs.updateTemp.validate((valid) => {
          if (valid) {
            console.log(this.updateTemp)
            this.$emit('update')
            this.$refs.updateTemp.resetFields()
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
